<template>
  <div class="server-main">
    <el-form :inline="true" v-model="modelData" class="search-form">
      <el-form-item label="模型分类" class="status form-input">
        <el-select v-model="modelData.modelType">
          <el-option label="全部" value=""></el-option>
          <el-option label="分类分析模型" value="FLMX"></el-option>
          <el-option label="聚类分析模型" value="JLMX"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button id="btn" size="small" type="primary" @click="onSubmit(1)"
          >查询</el-button
        >
      </el-form-item>
    </el-form>

    <div class="server-table">
      <div class="table-title">
        <span class="table-title-text">模型管理</span>
        <div>
          <!-- zhurh  22/1/14   统计分析按钮 -->
          <el-button type="primary" size="small" @click="openAnalysePop()"
            >统计分析</el-button
          >
          <el-button type="primary" size="small" @click="openCreateProperty"
            >上传模型</el-button
          >
        </div>
      </div>
      <el-table :data="tableList" class="km-table">
        <el-table-column
          type="index"
          label="ID"
          width="50"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="modelName"
          label="模型名称"
          align="center"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="modelTypeName"
          label="模型分类"
          align="center"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="modelVersion"
          label="版本"
          align="center"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="createTime"
          label="上传时间"
          align="center"
          show-overflow-tooltip
        ></el-table-column>
        <!-- <el-table-column
          prop="createUseId"
          label="上传人员"
          align="center"
          show-overflow-tooltip
        >
        </el-table-column> -->

        <el-table-column label="操作" align="center" width="200">
          <template slot-scope="scope">
            <el-link
              :underline="false"
              type="primary"
              class="ontology-edit"
              @click.native.prevent="openEditOntology(scope.row)"
              >配置</el-link
            >
            <el-link
              :underline="false"
              type="primary"
              class="ontology-edit"
              @click.native.prevent="applyRecord(scope.row.modelId)"
              >应用记录</el-link
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="km-pagination"
        background
        :small="true"
        layout="total, prev, pager, next,sizes, jumper"
        :total="total"
        :hide-on-single-page="!total"
        :page-size.sync="modelData.pageSize"
        :pager-count="7"
        :page-sizes="[10, 20, 30]"
        :current-page="modelData.pageNum"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
      >
      </el-pagination>
      <el-dialog
        :title="dialogTitle"
        :visible.sync="dialogVisible"
        @close="afterDialogClose"
      >
        <el-form
          :model="dialogForm"
          :rules="dialogRules"
          ref="dialogForm"
          label-position="right"
          label-width="160px"
          :inline="true"
        >
          <el-form-item
            label="选择文件:"
            prop="fileName"
            v-if="dialogTitle !== '参数配置'"
          >
            <el-upload
              action="#"
              v-if="!dialogForm.fileName"
              :before-upload="beforeUpload"
              :show-file-list="false"
              :http-request="uploadSectionFile"
            >
              <el-button type="primary">选择文件</el-button>
            </el-upload>
            <el-input v-else v-model="dialogForm.fileName" readonly></el-input>
          </el-form-item>
          <el-form-item label="模型名称:">
            <el-input v-model="dialogForm.modelType" readonly></el-input>
          </el-form-item>
          <el-form-item label="模型版本号:" prop="modelVersion">
            <el-input
              v-model="dialogForm.modelVersion"
              :readonly="dialogTitle !== '上传模型参数配置'"
            ></el-input>
          </el-form-item>
          <el-form-item label="模型类型:" prop="modelType">
            <el-select
              v-model="dialogForm.modelType"
              :disabled="dialogTitle !== '上传模型参数配置'"
              style="width: 202px"
            >
              <el-option label="分类模型" value="分类分析模型"></el-option>
              <el-option label="聚类模型" value="聚类分析模型"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="召回率:" prop="recallRate">
            <el-input v-model="dialogForm.recallRate"></el-input>
          </el-form-item>
          <span :style="{ height: '30px', lineHeight: '38px' }">%</span>
          <el-form-item label="准确率:" prop="exactRate">
            <el-input v-model="dialogForm.exactRate"></el-input>
          </el-form-item>
          <span :style="{ height: '30px', lineHeight: '38px' }">%</span>
        </el-form>
        <div slot="footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="dialogDefine(dialogTitle)"
            >确 定</el-button
          >
        </div>
      </el-dialog>
      <!-- zhurh  22/1/14   分析折线图弹窗 -->
      <el-dialog
        title="统计分析"
        v-if="analyseChartsVisible"
        :visible.sync="analyseChartsVisible"
        @close="afterDialogClose"
        width="70%"
        top="8vh"
        custom-class="dialog-warpper"
      >
        <el-select
          v-model="chartModelType"
          placeholder="请选择"
          @change="getModelList($event)"
        >
          <el-option
            v-for="item in selectClassData"
            :key="item.id"
            :label="item.title"
            :value="item.id"
          >
          </el-option>
        </el-select>
        <div class="el-dialog-div">
          <LineCharts
            id="analiyseChart"
            :dataList="chartsList"
            :modalType="chartModelType"
          >
          </LineCharts>
        </div>
        <div slot="footer" class="dialog-footer">
          <el-button @click="resetDialogForm" type="primary">关 闭</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { mapState, mapMutations } from "vuex";
import LineCharts from "../../components/charts/lineChart.vue";
export default {
  name: "intelligenceRec",
  components: { LineCharts },
  data: () => {
    return {
      modelId: "", // 属性 id
      modelData: {
        modelCategory: "JGGL", // 结果管理
        pageNum: 1, // 页码
        pageSize: 10, // 每页条数
      },
      selectClassData: [
        //zhurh --类别选择数据或者从后台获取数据
        { id: "FLMX", title: "分类分析模型" },
        { id: "JLMX", title: "聚类分析模型" },
      ],
      chartsList: [], //zhurh --图表数据
      chartModelType: "FLMX", //zhurh --默认类型
      tableList: [], // 本体类别列表
      total: 0, // 数据总条数
      analyseChartsVisible: false, //zhurh -- 分析柱状图弹窗显隐控制变量
      dialogVisible: false,
      dialogTitle: "参数配置",
      dialogForm: {
        fileName: "",
        recommedModel: "工具推荐模型",
        modelVersion: "",
        recallRate: "",
        exactRate: "",
        modelType: "",
      },
      dialogRules: {
        fileName: [
          {
            required: true,
            message: "请选择文件",
            trigger: "blur",
          },
        ],
        modelVersion: [
          {
            required: false,
            message: "请输入模型版本号",
            trigger: "blur",
          },
        ],
        recallRate: [
          {
            required: true,
            message: "请输入召回率",
            trigger: "blur",
          },
        ],
        exactRate: [
          {
            required: true,
            message: "请输入准确率",
            trigger: "blur",
          },
        ],
      },
    };
  },
  computed: {
    ...mapState({
      containerDom: (state) => state.server.containerDom,
    }),
  },
  async created() {
    this.getTableList();
  },
  methods: {
    testprops() {
      this.testprop++;
    },
    // 查询
    onSubmit(pageNum) {
      this.modelData.pageNum = pageNum ? pageNum : this.modelData.pageNum;
      this.getTableList();
    },

    // 获取本体模型属性列表
    async getTableList() {
      const { pageNum, pageSize, modelCategory, modelType } = this.modelData;
      let params = {
        modelCategory,
        pageNum,
        pageSize,
        modelType,
      };
      let res = await this.$axios.post(
        this.$api.resultAnalyse.zehaoObj.modelList,
        params
      );
      if (res.data.code !== 200) {
        this.$message.warning(res.data.message);
        return;
      }
      this.total = res.data.data.total;
      this.tableList = res.data.data.list;
    },

    // 修改当前页码
    handleCurrentChange(page) {
      this.modelData.pageNum = page;
      this.getTableList();
    },
    //修改当前页面容量
    handleSizeChange(page) {
      this.modelData.pageSize = page;
      this.getTableList();
    },
    //跳转应用记录
    applyRecord(modelId) {
      this.$router.push({
        path: "/resultAnalyse/modelManagementHistory",
        query: {
          modelId,
        },
      });
    },

    // 配置
    openEditOntology(row) {
      this.dialogTitle = "参数配置";
      this.dialogVisible = true;
      const {
        modelId,
        modelType,
        modelName,
        modelVersion,
        exactRate,
        recallRate,
      } = row;
      this.modelId = modelId;
      this.dialogForm.modelName =
        modelType === "FLMX" ? "分类分析模型" : "聚类分析模型";
      this.dialogForm.modelVersion = modelVersion;
      this.dialogForm.exactRate = exactRate;
      this.dialogForm.recallRate = recallRate;
      this.dialogForm.modelType =
        modelType === "FLMX" ? "分类分析模型" : "聚类分析模型";
    },

    // 上传模型
    openCreateProperty() {
      this.dialogTitle = "上传模型参数配置";
      this.dialogVisible = true;
      this.dialogForm.modelName = "";
      this.dialogForm.modelVersion = "";
      this.dialogForm.recallRate = "";
      this.dialogForm.exactRate = "";
      this.dialogForm.recommedModel = "";
      this.dialogForm.modelType = "";
    },

    // 配置函数
    tableConfig() {
      let { modelType, modelName, modelVersion, exactRate, recallRate } =
        this.dialogForm;
      let params = {
        modelId: this.modelId,
        modelName,
        modelType: modelType === "分类分析" ? "FLMX" : "JLMX",
        modelVersion,
        recallRate,
        exactRate,
      };

      //  let res = this.$axios.post(this.$api.resultAnalyse.zehaoObj.updateModel, params);
      this.$axios
        .post(this.$api.resultAnalyse.zehaoObj.updateModel, params)
        .then((res) => {
          this.$message.warning(res.data.message);
          this.dialogVisible = false;
          this.getTableList();
        })
        .catch((error) => {
          this.loading = false;
        });
    },

    // 上传文件
    async uploadSectionFile() {
      let params = {
        file: this.uploadFile,
      };
      let res = await this.$fileHttp.uploadFile(
        this.$api.resultAnalyse.zehaoObj.fileUpload,
        params
      );
      this.modelUrl = res.data;
    },

    // 上传模型
    async uploadModel() {
      let { recommedModel, modelVersion, recallRate, modelType, exactRate } =
        this.dialogForm;
      let params = {
        modelCategory: "JGGL",
        modelName: modelType,
        modelType: modelType === "分类分析模型" ? "FLMX" : "JLMX",
        modelVersion,
        recallRate,
        exactRate,
        modelUrl: this.modelUrl,
      };
      let res = await this.$axios.post(
        this.$api.resultAnalyse.zehaoObj.insertModel,
        params
      );
      if (res.data.code !== 200) {
        let message = res.data.message;
        this.$message.warning(message);
        this.dialogVisible = false;
      } else {
        this.$message.warning(res.data.message);
        this.dialogVisible = false;
      }
      this.dialogVisible = false;
      this.getTableList();
    },

    // 上传文件前保存文件名和文件对象
    beforeUpload(file) {
      this.dialogForm.fileName = file.name;
      this.uploadFile = file;
      return false;
    },

    // 模态框关闭后
    afterDialogClose() {
      this.$refs.dialogForm && this.$refs.dialogForm.resetFields();
    },

    // 点击确定后执行
    dialogDefine(title) {
      this.$refs.dialogForm.validate(async (valid) => {
        if (valid) {
          switch (title) {
            case "参数配置":
              this.tableConfig();
              break;
            case "上传模型参数配置":
              await this.uploadSectionFile();
              if (this.modelUrl) {
                this.uploadModel();
              }
              break;
          }
        }
      });
    },
    // zhurh--打开分析柱状图弹框
    openAnalysePop(row) {
      this.getModelList("FLMX");
    },
    // zhurh--获取分析折线图数据
    getModelList(type) {
      this.$axios
        .get(
          `${this.$api.resultAnalyse.zehaoObj.modelAnalyse}?modelType=${type}`
        )
        .then((res) => {
          this.chartsList = res.data.data;
          this.analyseChartsVisible = true;
        })
        .catch((err) => {
          console.error(err);
        });
    },
    // 取消
    resetDialogForm() {
      this.dialogVisible = false;
      this.analyseChartsVisible = false;
    },
  },
};
</script>
<style>
.dialog-warpper {
  padding: 20px;
}
</style>
<style lang="scss" scoped>
.el-dialog-div {
  height: 60vh;
  overflow: hidden;
  // padding: 20px;
}
.server-main {
  position: relative;

  .search-form {
    background: #ffffff;
    border-radius: 4px;
    padding: 16px 32px;

    .el-form-item {
      margin-bottom: 0;
    }

    .form-input {
      margin-right: 32px;

      .el-input,
      .el-select {
        ::v-deep .el-input__inner {
          width: 240px;
          height: 32px;
          background: #ffffff;
          border-radius: 2px;
          border: 1px solid #e0e0e0;
        }
      }
    }
  }

  .server-table {
    margin-top: 16px;
    padding: 16px 32px 64px;
    background: #ffffff;
    border-radius: 4px;

    .table-title {
      display: flex;
      justify-content: space-between;
      align-items: center;

      .table-title-text {
        margin: 8px 0;
        font-size: 16px;
        line-height: 16px;
        border-left: 5px solid $--color-primary;
        padding-left: 10px;
      }

      margin-bottom: 10px;
    }
  }

  .ontology-edit {
    padding: 0 10px;

    & + .ontology-edit {
      border-left: 1px solid #dddddd;
    }
  }

  ::v-deep .el-table__append-wrapper {
    padding: 10px;
  }

  ::v-deep .el-dialog {
    width: 500px;
    border-radius: 5px;
  }

  .dialog-inline {
    ::v-deep .el-form-item__content {
      width: 300px;
    }

    button.el-button {
      background-color: $--color-primary !important;
      color: #fff !important;
      line-height: 1.2;
    }
  }
}
</style>
